package Controllers;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Models.Absence;
import Models.Etudiant;
import Models.Module;


@SuppressWarnings("serial")
public class EtudiantsController extends MainController {
	
	String listView, addView, viewView;
	
	// INIT
	public void init() throws ServletException {
		this.listView 	= this.getInitParameter("view_list");
		this.addView 	= this.getInitParameter("view_add");
		this.viewView 	= this.getInitParameter("view_view");
	}
	
	// POST
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String method = request.getMethod().toLowerCase();
		String action = request.getPathInfo();
		if (action == null) {
			action = "/";
			System.out.println("action == null");
		}
		if (method.equals("post") && action.equals("/add")) {
			request.setAttribute("title", "Ajouter un étudiant");
			this.add(request, response);
		}
	}
	
	// GET
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// On récupère la méthode d'envoi de la requête
		String method = request.getMethod().toLowerCase();
		
		// On récupère l'action à exécuter
		String action = request.getPathInfo();
		if (action == null) {
			action = "/";
			System.out.println("action == null");
		}

		if (method.equals("get") && action.equals("/list")) {
			request.setAttribute("title", "Liste des étudiants");
			this.list(request, response);
		} else if (method.equals("get") && action.equals("/add")) {
			request.setAttribute("title", "Ajouter un étudiant");
			this.add(request, response);
		} else if (method.equals("get") && action.equals("/view")) {
			this.view(request, response);
		}
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HashSet<Etudiant> etuList = new HashSet<Etudiant>();
		etuList.add(new Etudiant(1, 1, "Pierre-Julien", "MARTINEZ", null, null, "MIAM", null));
		etuList.add(new Etudiant(2, 1, "Florian", "PARRENO", null, null, "MIAM", null));
		etuList.add(new Etudiant(3, 1, "Tangui", "RIONDET", null, null, "MIAM", null));
		etuList.add(new Etudiant(4, 1, "Kevin", "DIAZ", null, null, "MIAM", null));
		etuList.add(new Etudiant(5, 1, "Thomas", "DONZELLE", null, null, "MIAM", null));
		
		request.setAttribute("etuList", etuList);
		this.loadJSP(this.listView, request, response);
	}
	
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.loadJSP(this.addView, request, response);
	}
	
	private void view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Integer id_etudiant = (Integer) request.getAttribute("etudiant");
		Etudiant etu = new Etudiant(6, 1, "Kevin", "DIAS", null, null, "MIAM", null);
		
		HashSet<Absence> absences = new HashSet<Absence>(); 
		absences.add(new Absence(1, 6, new Date(), "medecin"));
		absences.add(new Absence(2, 6, new Date(), null));
		
		HashSet<Module> modules = new HashSet<Module>(); 
		modules.add(new Module(1, "SIL-4", "Programmation Web en Java", null, null));
		modules.add(new Module(2, "INFO-2", "Traitement des images", null, null));

		etu.setAbsences(absences);
		etu.setModules(modules);
		
		request.setAttribute("etudiant", etu);
		request.setAttribute("title", etu.getNom());
		this.loadJSP(this.viewView, request, response);
	}
}
